package com.example.demospringboot;

import org.flowable.engine.*;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.form.api.FormRepositoryService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zhaoPin
 * @Date 2024/7/23  16:36
 * @description: 动态表单
 * 实际的工作流环境中是否要使用表单
 * 1.简单的数据我们可以使用表单
 * 2.复杂的数据我们直接使用业务主键即可
 */

@SpringBootTest
public class Flowable07Back {

    @Autowired
    ProcessEngine processEngine;

    @Autowired
    RepositoryService repositoryService;

    @Autowired
    RuntimeService runtimeService;

    @Autowired
    TaskService taskService;

    @Autowired
    FormRepositoryService formRepositoryService;

    @Autowired
    FormService formService;

    @Autowired
    IdentityService identityService;


    /**
     * 串行流程部署操作
     */
    @Test
    void deployFlow() throws InterruptedException {
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("process/07-back/back-demo2.bpmn20.xml")
                .name("并行流程回退")
                .deploy();// 部署的方法
        System.out.println("*******  deploy.getId() = " + deploy.getId());
    }

    /**
     * 并行流程部署操作
     */
    @Test
    void parallelDeployFlow() throws InterruptedException {
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("process/07-back/back-demo1.bpmn20.xml")
                .name("并行流程回退")
                .deploy();// 部署的方法
        System.out.println("*******  deploy.getId() = " + deploy.getId());
    }

    /**
     * 启动流程实例
     * vkaffbzroxqgbghd
     */
    @Test
    void startFlow() throws InterruptedException {
        // 在流程定义表中动态维护
        String processId = "back-demo2:1:788b284c-651c-11ef-8104-b469218e373e";

        /**
         * 参数记录到
         * SELECT * FROM `act_ru_variable`
         */
        Map<String, Object> map = new HashMap<>();
        // 1.根据流程定义ID启动流程实例
        map.put("users", Arrays.asList("张三24", "李四24"));
        map.put("reason", "行到水穷处");

        /**
         * 记录流程的发起人信息
         * SELECT * FROM `act_ru_identitylink`
         */
        identityService.setAuthenticatedUserId("userId:xxx");
        ProcessInstance processInstance = runtimeService.startProcessInstanceById(processId, map);
        System.out.println("processInstance.getId() = " + processInstance.getId());
    }

    /**
     * 任务的审批
     */
    @Test
    void completeTask() throws InterruptedException {
        Map<String, Object> map = new HashMap<>();
        map.put("flag", false);
        taskService.complete("3789ba6d-4a65-11ef-b31a-b469218e373e", map);
    }

    /**
     * 串行 流程回退 或 跳过
     */
    @Test
    void skipFlow(){
        runtimeService.createChangeActivityStateBuilder()
                // 绑定到具体的流程实例
                .processInstanceId("0a5ed743-4a63-11ef-bd1f-b469218e373e")
                // 从 A 跳转到 B ，即可以回退 有可以向前跳 串行流程回退
                // .moveActivityIdTo("user4","user1")
                // 并行流程回退到指定节点
                .moveActivityIdsToSingleActivityId(Arrays.asList("usertask4"),"usertak1")
                .changeState();

        // 完成流程回退后 可以从 act_hi_actinst、 act_hi_taskinst
    }

    /**
     * 并行 流程回退 到网关
     */
    @Test
    void skipFlow01(){
        runtimeService.createChangeActivityStateBuilder()
                // 绑定到具体的流程实例
                .processInstanceId("0a5ed743-4a63-11ef-bd1f-b469218e373e")
                // 并行流程回退到网关，即回退到多个节点
                .moveSingleActivityIdToActivityIds("usertask5",Arrays.asList("usertask4","usertask2"))
                .changeState();

        // 完成流程回退后 可以从 act_hi_actinst、 act_hi_taskinst
    }


    /**
     * 撤销流程实例
     */
    @Test
    void deleteProcessInstance(){
        // 完成流程撤销后 可以从 act_hi_actinst、 act_hi_taskinst 中查看记录
        runtimeService.deleteProcessInstance("0a5ed743-4a63-11ef-bd1f-b469218e373e","测试删除内容");
    }


}
